package cn.bugstack.mybatis.mapping;

import cn.bugstack.mybatis.session.Configuration;
import cn.bugstack.mybatis.type.JdbcType;
import cn.bugstack.mybatis.type.TypeHandler;
import cn.bugstack.mybatis.type.TypeHandlerRegistry;

/**
 * @author wcc
 * @description 参数映射 #{property,javaType=int,jdbcType=NUMERIC}
 * @create
 */

public class ParameterMapping {
    private Configuration configuration;
    //property
    private String property;
    //javaType
    private Class<?> javaType = Object.class;
    //jdbcType
    private JdbcType jdbcType;
    private TypeHandler<?> typeHandler;
    private ParameterMapping() {
    }
    public static class Builder {
        private ParameterMapping parameterMapping = new ParameterMapping();
        public Builder(Configuration configuration, String property, Class<?> javaType){
            parameterMapping.configuration = configuration;
            parameterMapping.property = property;
            parameterMapping.javaType = javaType;
        }
        public Builder javaType(Class<?> javaType){
            parameterMapping.javaType = javaType;
            return this;
        }
        public Builder jdbcType(JdbcType jdbcType){
            parameterMapping.jdbcType = jdbcType;
            return this;
        }
        public ParameterMapping build(){
            if (parameterMapping.typeHandler == null && parameterMapping.javaType != null){
                Configuration configuration = parameterMapping.configuration;
                TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
                parameterMapping.typeHandler = typeHandlerRegistry.getTypeHandler(parameterMapping.getJavaType(), parameterMapping.getJdbcType());
            }
            return parameterMapping;
        }

    }
    public Configuration getConfiguration() {
        return configuration;
    }

    public TypeHandler<?> getTypeHandler(){
        return typeHandler;
    }

    public String getProperty() {
        return property;
    }

    public Class<?> getJavaType() {
        return javaType;
    }

    public JdbcType getJdbcType() {
        return jdbcType;
    }

}
